package com.tiger.cloud.service.crm;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tiger.cloud.common.exception.ServiceException;
import com.tiger.cloud.common.mapper.JsonMapper;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.dao.crm.MailListDao;
import com.tiger.cloud.dao.crm.RecommandListDao;
import com.tiger.cloud.model.crm.MailList;
import com.tiger.cloud.model.ext.RecommandListMatch;
import com.tiger.cloud.util.DateTimeUtil;
import com.tiger.cloud.util.ErrorCode;

/**
 * 用户通讯录
 */
@Service("mailListService")
public class MailListService extends TigerBaseService<MailList> implements IMailListService{
    
    @Autowired
	private MailListDao mailListDao;
    @Autowired
    private IUserService userService;
    @Autowired
    private RecommandListDao recommandListDao;
    
	@Override
	public MybatisTigerDao<MailList> getEntityDao() {
		return mailListDao;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean addMailLists(List<MailList> mailLists, long userId) {
		int count=mailLists.size();
		MailList mailList = new MailList();
		mailList.setUserId(userId);
		mailListDao.removeByEntity(mailList);
		if(mailListDao.insertBatch(mailLists)!=count){
			throw new ServiceException(ErrorCode.INSERT_DATA_ERROR);
		}
		return true;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public Integer runMailListsMatch(Long userId) {
		Date currentTime=DateTimeUtil.getSystemDate();//当前系统时间
		Integer count= mailListDao.runMailListsMatch(userId);	
		MailList mailList = new MailList();
		mailList.setUserId(userId);
		mailListDao.removeByEntity(mailList);
		if(count>0){
//			System.out.println("currentTime  "+JsonMapper.nonEmptyMapper().toJson(currentTime));
//			List<RecommandListMatch> recommandListMatchs=recommandListDao.getRecommandListMatch(currentTime);
//			if(recommandListMatchs!=null&&recommandListMatchs.size()>0){
//				List<String> aliases=new ArrayList<String>();
//				for (RecommandListMatch recommandListMatch : recommandListMatchs) {
//					if((!StringUtils.isEmpty(recommandListMatch.getJpushCode())) && recommandListMatch.getJpushCode().length()>10){
//						aliases.add(recommandListMatch.getJpushCode());
//					}
//				}
//			}
			return count;
		}
		return 0;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void clearMailListsMatch() {
		mailListDao.clearMailListsMatch();
	}

}